package com.metalrain.alm.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import com.metalrain.alm.AndroidLightManagerApplication;
import com.metalrain.alm.R;
import com.metalrain.alm.service.LCA_Service;
import com.metalrain.alm.ui.adapters.MyLightsAdapter;
import com.philips.lighting.model.PHLight;


public class LightOptionsActivity extends Activity {
    MyLightsAdapter myLightsAdapter;
    public static final String POSITION_IN_LIST = "POSITION_IN_LIST";
    private int mPositionInList;

    TextView title;
    View btnPower, btnRand, btnWarm, btnCold, btnBright, btnDim;
    CheckBox similarLights;
    private Object backingObject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_light_options);

        similarLights = (CheckBox)findViewById(R.id.similarCheckbox);
        myLightsAdapter = new MyLightsAdapter(this);

        mPositionInList = getIntent().getIntExtra(POSITION_IN_LIST,-1);
        if (mPositionInList == -1) throw new IllegalStateException("Position not provided, so what are we changing?");

        backingObject = myLightsAdapter.getItem(mPositionInList);
        if (backingObject instanceof String) similarLights.setVisibility(View.GONE);        //Hide if String backing

        title = (TextView) findViewById(R.id.title);
        if (backingObject instanceof PHLight) {
            title.setText(((PHLight)myLightsAdapter.getItem(mPositionInList)).getName());

        } else {
            title.setText(myLightsAdapter.getItem(mPositionInList).toString());

        }

        //Hide the similar lights checkbox if it's a String backing (Probably all lights)
        btnPower = findViewById(R.id.off);
        btnRand = findViewById(R.id.randomize);
        btnWarm = findViewById(R.id.warm);
        btnCold = findViewById(R.id.cold);
        btnBright = findViewById(R.id.bright_up);
        btnDim = findViewById(R.id.bright_down);

        final LCA_Service service = AndroidLightManagerApplication.getLcaService();
        btnPower.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (backingObject instanceof PHLight) {
                    service.mHueManager.togglePower((PHLight) backingObject, similarLights.isChecked());
                } else {
                    service.mHueManager.turnOffAllLights();
                }
            }
        });

        btnRand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (backingObject instanceof PHLight) {
                        service.mHueManager.randLight((PHLight) backingObject, similarLights.isChecked());
                } else {
                    service.mHueManager.randAllLights();
                }
            }
        });

        btnWarm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (backingObject instanceof PHLight) {
                    service.mHueManager.warmLight((PHLight)backingObject, similarLights.isChecked());
                } else {
                    service.mHueManager.warmAll();
                }
            }
        });

        btnCold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (backingObject instanceof PHLight) {
                    service.mHueManager.coldLight((PHLight) backingObject, similarLights.isChecked());
                } else {
                    service.mHueManager.coldAll();
                }
            }
        });

        btnBright.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (backingObject instanceof PHLight) {
                    service.mHueManager.adjustBrightness((PHLight)backingObject,25, similarLights.isChecked());
                } else {
                    service.mHueManager.adjustAllBrightness(25);
                }
            }
        });

        btnDim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (backingObject instanceof PHLight) {
                    service.mHueManager.adjustBrightness((PHLight) backingObject, -25, similarLights.isChecked());
                } else {
                    service.mHueManager.adjustAllBrightness(-25);
                }
            }
        });

    }

    public static void start(Activity activity, int i) {
        Intent intent = new Intent(activity, LightOptionsActivity.class);
        intent.putExtra(POSITION_IN_LIST, i);
        activity.startActivity(intent);

    }
}
